package controllers

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"week2d/models/mysql"
	"week2d/models/request"
)

// 新增课程
func GoodsCreate(c *gin.Context) {
	var goods request.GoodsCreateWeek2d
	if err := c.ShouldBindJSON(&goods); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	err := mysql.GoodsCreate(goods)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"msg": "新增失败",
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "新增成功",
	})
}

// 课程详情
func GoodsDetail(c *gin.Context) {
	var goods request.GoodsDetailWeek2d
	if err := c.ShouldBindJSON(&goods); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	detail := mysql.GoodsDetail(goods.GoodsID)

	c.JSON(http.StatusOK, gin.H{
		"msg":  "查询成功",
		"data": detail,
	})

}

// 删除课程详情
func GoodsDelete(c *gin.Context) {
	var goods request.GoodsDeleteWeek2d
	if err := c.ShouldBindJSON(&goods); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	err := mysql.GoodsDelete(goods.GoodsID)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"msg": "删除失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"msg": "删除成功",
	})

}
